#include<iostream>
using namespace std;
#include<string>
// push_back() 插入字符
void test_string1()
{
	string s;
	s.push_back('C');
	s.push_back('S');
	s.push_back('D');
	s.push_back('N');
	cout << s << endl;
	cout << s.capacity() << endl;
	cout << s.size() << endl << endl;

	string s1("hello ");
	s1.push_back('W');
	cout << s1 << endl;
}
// append()插入字符串
void test_string2()
{
	string s1("I ");
	string s2("like");

	// append(string)完成两个string类的对象拼接;
	s1.append(s2);
	cout << s1 << endl; // I like
	
	// append(str)在字符串后面追加字符串" C++";
	s1.append(" C++");
	cout << s1 << endl; // I like C++

	// append(n,char) 将n个字符char 追加到字符串string类对象中
	s1.append(3, '!');
	cout << s1 << endl;// I like C++!!!
}
void test_string3()
{
	string s1("hello");
	s1 += ' ';
	cout << s1 << endl;// hello		

	s1 += 'C';
	cout << s1 << endl; // hello C

	s1 += "++";
	cout << s1 << endl;
}
// pop_back() 删除
void test_string4()
{
	string s1("hello");
	cout << s1 << endl;
	s1.pop_back();
	s1.pop_back();
	cout << s1 << endl; // hel
}
// c_str
void test_string5()
{
	string s1("hello string");
	cout << "调用流插入打印: " << s1 << endl; // 调用重载的流插入运算符打印的;
	cout << "调字符串打印: " << s1.c_str() << endl; // 调用字符串打印，s_str 是遇到斜杠0 '\0'结束的;
}
void test_string()
{
	string s1("hello world");
	s1.push_back('!');
	cout << s1 << endl;
	s1.append("hello bit");
	cout << s1 << endl;
	s1.append(10, 'x');
	cout << s1 << endl;

	string s2(" apple ");
	s1.append(++s2.begin(), --s2.end());
	cout << s1 << endl;
	//s1.append(s2);
	//cout << s1 << endl;
}

void test_string6()
{
	string s1("xhello world");
	cout << s1 << endl;
	
	// 为字符串分配新值，并替换当前内容
	s1.assign(" xxxxxx");
	cout << s1 << endl;

	// 头插
	s1.insert(0, "yyyyyy");
	cout << s1 << endl;
	
	// 在第5个位置删除3个字符
	s1.erase(5, 3);
	cout << s1 << endl;
	// 删除所有字符
	//s1.erase();
	//cout << s1 << endl;

	// 删除begin+1到end的字符
	s1.erase(s1.begin() + 1, s1.end());
	cout << s1 << endl;

	// replace 用20%取代从第5个字符开始的第一个字符
	string s2("hello world hello bit");
	s2.replace(5, 1, "20%");
	cout << s2 << endl;
}
void test_string7()
{
	string s1("hello world hello bit");
	cout << s1 << endl;
	size_t pos = s1.find(' ');
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "20%");
		pos = s1.find(' ');
	}
	cout << s1 << endl;
}
int main()
{
	//test_string();
	//test_string1();
	//test_string2();
	//test_string3();
	//test_string4();
	//test_string5();
	//test_string6();
	test_string7();
	return 0;
}